<?php
	
function list_jabatan_physical(){

//PASANG JAVASCRIPT
my_set_code_js('

function confirmDelete(id){
	var t = confirm(\'Yakin akan menghapus data ?\');
	if(t){
		location.href=\'index.php?com=jabatan_physical&task=delete&id=\'+id;
	}
	return false;
}

');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Spesifikasi kualifikasi'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Penilaian level'=>array('style'=>'border-bottom:2px solid;width:55%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10'), 
	);

	$query = "SELECT * FROM jabatan_physical";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=jabatan_physical&task=editjabatan_physical&id='.$ey['physical_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['physical_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'jabatan_physical'=>  $ey['nama_physical'] ,  
			'cat'=>  $ey['catatan'] ,   
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);   
	return table_builder($header , $datas ,  6 , false );
}

function form_jabatan_physical_create($id = 0){
	$view = form_header( "jabatan_physical" , "jabatan_physical"  );
 	$fields = my_get_data_by_id('jabatan_physical','physical_id',$id);
	$nama = array(
				'name'=>'nama_physical',
				'value'=>( isset($_POST['nama_physical']) ? $_POST['nama_physical'] : $fields['nama_physical'] ),
				'id'=>'nama_physical',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	
	$view .= form_field_display( $form_field_nama   , "Spesifikasi"   );
	
	$opsi = array(
		'Ya'=>'Ya atau Tidak',
		'Opsi'=>'Pilihan' 
	);
	$tipe_jawaban = array(
				'name'=>'tipe_jawaban',
				'value'=>( isset($_POST['tipe_jawaban']) ? $_POST['tipe_jawaban'] : $fields['tipe_jawaban'] ),
				'id'=>'tipe_jawaban' 
			);
	$form_field_jawaban = form_radiobutton($tipe_jawaban ,$opsi);
	
	$view .= form_field_display( $form_field_jawaban   , "Tipe Jawaban"   );
	
	$catatan = array(
				'name'=>'catatan',
				'value'=>( isset($_POST['catatan']) ? $_POST['catatan'] : $fields['catatan'] ),
				'id'=>'catatan' 
			);
	$form_field_catatan = form_textarea($catatan);
	
	$view .= form_field_display( $form_field_catatan .'<font size="1">
	<br /><b>Jika tipe adalah pilihan:</b>
	<br/>(Pilihan1,skor1)|(Pilihan2,skor2)|(Pilihan3,skor3)
	<br />*Dimana skor menunjukan tingkat kualifikasi</font>'   , "Jawaban skor"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_jabatan_physical_validate($id ){
	$errsubmit = false;
	$err = array();

	$code =  trim($_POST["nama_physical"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Spesifikasi belum di isi";
	}

	$query="SELECT physical_id FROM jabatan_physical 
		WHERE nama_physical='".$code.
		"' AND physical_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kode sudah digunakan";	
	}
	if($_POST['tipe_jawaban'] == 'Opsi'){
		$list_answer = explode("|", $_POST['catatan']);
		foreach($list_answer as $answer){
			$answer = trim($answer);
			if(strpos($answer, '(' ) === false){
				$errsubmit = true;
				$err[] = "Pola formasi jawaban belum benar";
			}
			else if(strpos($answer, ')' ) === false){
				$errsubmit = true;
				$err[] = "Pola formasi jawaban belum benar";
			}
			else if(strpos($answer, ',' ) === false){
				$errsubmit = true;
				$err[] = "Pola formasi jawaban belum benar";
			}
			$list_jawaban_skor = explode("," , $answer);
			if( count($list_jawaban_skor) <> 2 ){
				$errsubmit = true;
				$err[] = "Pola formasi jawaban belum benar";
				break;
			}
		}
		
	}
	
	$code =  trim($_POST["tipe_jawaban"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Tipe Jawaban belum di isi";
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function form_jabatan_physical_submit($id){
	$jawaban = $_POST['catatan'];
	if($_POST['tipe_jawaban'] == 'Ya'){
		$jawaban = 'Ya|Tidak';
	}
	$datas = array(
		'nama_physical'=>my_type_data_str( $_POST['nama_physical'] ),
		'tipe_jawaban'=>my_type_data_str( $_POST['tipe_jawaban'] ),
		'catatan'=>my_type_data_str($jawaban),
		'datetime_added'=>my_type_data_function('NOW()'),
	);
	if($id > 0){
		return my_update_record('jabatan_physical' ,'physical_id' , $id , $datas);
	}
	
	return my_insert_record('jabatan_physical' , $datas);
}

function remove_jabatan_physical($id){
	$query= "DELETE FROM jabatan_physical WHERE physical_id = {$id}";
	return my_query($query);
} 


function add_link_jabatan($jabatan_id){
	$view = form_header( "jabatan_physical" , "jabatan_physical"  );
	$form_field_list="";
	$query = "SELECT * FROM jabatan_physical";
	$result = my_query($query);
	$specs = array();
	while($row = my_fetch_array($result)){
			$specs[$row['physical_id']] = $row['nama_physical'];
	}
	$f = array(
		'name'=>'physical_id',
		'id'=>'physical_id',
		'value'=>isset($_POST['physical_id']) ? $_POST['physical_id'] : 0,
	);
	 $form_field_da = form_dropdown($f , $specs);
	$view .= form_field_display( $form_field_da   , "Spesifikasi"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function add_link_jabatan_submi($jabatan_id){
	$datas = array(
		'jabatan_id'=>my_type_data_int($jabatan_id),
		'jabatan_physical_id'=>my_type_data_int($_POST['jabatan_physical_id']),
	);
	
	return my_insert_record( 'jabatan_spesifikasi_link' , $datas);
} 